/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.factory.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.FactoryException;
import com.pitop.factory.dao.FactoryDAO;
import com.pitop.factory.entity.Factory;
import com.pitop.factory.entity.FactoryState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("factorySV")
@Slf4j
public class FactorySVImpl extends BaseSVImpl<Factory, Long> implements FactorySV {

    @Autowired
    private FactoryDAO factoryDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return factoryDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     */
    @Override
    public void save(Factory entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setState(FactoryState.Enable.name());
        entity.setVer(1);
        super.save(entity);
    }

    /**
     * 数据更新
     *
     * @param factory
     */
    @Override
    public void modify(Factory factory) throws BaseException {
        int oldVer = loadByCode(factory.getCode()).getVer();
        factory.setVer(oldVer + 1);
        factory.setUpdateTime(new Date());
        Map<String, Object> paraMap = JSON.parseObject(JSON.toJSONString(factory, SerializerFeature.WriteDateUseDateFormat));
        paraMap.put("oldVer", oldVer);
        getBaseDAO().update(paraMap);
    }

    /**
     * 加载一个对象Factory
     *
     * @param id id* @param code 工厂编码
     * @return Factory
     */
    @Override
    public Factory load(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return factoryDAO.load(param);
    }

    /**
     * 加载一个对象Factory 通过id
     *
     * @param id id
     * @return Factory
     */
    @Override
    public Factory loadById(java.lang.Long id) {
        if (id == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return factoryDAO.loadById(id);
    }

    /**
     * 加载一个对象Factory 通过code
     *
     * @param code 工厂编码
     * @return Factory
     */
    @Override
    public Factory loadByCode(java.lang.String code) {
        if (code == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return factoryDAO.loadByCode(code);
    }


    /**
     * 根据主键id,oldStates 共同更新 Factory 的状态到newState状态
     *
     * @param id        id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, FactoryState newState, FactoryState... oldStates) {
        if (id == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        factoryDAO.updateStateById(id, newState.name(), states);
    }

    /**
     * 根据主键code,oldStates 共同更新 Factory 的状态到newState状态
     *
     * @param code      工厂编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(java.lang.String code, FactoryState newState, FactoryState... oldStates) {
        if (code == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        factoryDAO.updateStateByCode(code, newState.name(), states);
    }

    /**
     * 根据主键id 更新 Factory 的状态到另一个状态
     *
     * @param id    id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, FactoryState state) {
        if (id == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        factoryDAO.updateById(id, state.name());
    }

    /**
     * 根据主键code 更新 Factory 的状态到另一个状态
     *
     * @param code  工厂编码
     * @param state 状态
     */
    @Override
    public void updateByCode(java.lang.String code, FactoryState state) {
        if (code == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        factoryDAO.updateByCode(code, state.name());
    }


    /**
     * 删除对象Factory
     *
     * @param id   id
     * @param code 工厂编码
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new FactoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        factoryDAO.delete(param);
    }


    /**
     * 查询Factory分页
     *
     * @param factory 对象
     * @param offset  查询开始行
     * @param limit   查询行数
     * @return List<Factory>
     */
    @Override
    public List<Factory> list(Factory factory, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = new HashMap<>();
        if (factory != null) {
            map = JSON.parseObject(JSON.toJSONString(factory, SerializerFeature.WriteDateUseDateFormat));
        }
        map.put("sortColumns", " code desc ");
        return factoryDAO.list(map, new RowBounds(offset, limit));
    }

    /**
     * 统计
     *
     * @param factory 工厂实体
     * @return int
     */
    @Override
    public int count(Factory factory) {
        Map<String, Object> map;
        if (factory != null) {
            map = JSON.parseObject(JSON.toJSONString(factory, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashedMap();
        }
        return factoryDAO.count(map);
    }

    /**
     * 查询Factory分页
     *
     * @param id     id* @param code  工厂编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Factory>
     */
    @Override
    public List<Factory> list(java.lang.Long id, java.lang.String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return factoryDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return factoryDAO.count(param);
    }

}
